from BeautifulSoup import BeautifulSoup as BS
from elementtree import ElementTree as ET

def generate_html():
  
  ships=[]
  wikiwords=[]
  
  shipsTiddlerFormat="""<div title="%s" 
  modifier="Vittorios" 
  created="200806251141" 
  tags="Ship" changecount="1"><pre>
  \n%s\n
  </pre>
  </div>"""

  
  tiddlerFormat="""<div title="%(shipName)s" 
  modifier="Vittorios" 
  created="200806251141" 
  tags="Ship %(shipTags)s" 
  changecount="1">
  <pre>%(shipDescription)s

&lt;&lt;tabs txtFavourite
Summary &quot;Summary&quot; SummaryShipID%(shipId)s
Full &quot;Full&quot; FullShipID%(shipId)s
&gt;&gt; 

  </pre></div>

  <div title="SummaryShipID%(shipId)s" modifier="Vittorios" created="200806251141" tags="ShipDetails" changecount="1">
  <pre>
  \n!!!Base Damage Resists \n%(shipResistTable)s
  \n!!!Speed Mass Inertia \n%(shipSpeedTable)s
  </pre>
  </div>

  <div title="FullShipID%(shipId)s" modifier="Vittorios" created="200806251141" tags="ShipDetails" changecount="1">
  <pre>
  \n%(shipDetailsTable)s
  </pre>
  </div>
    """
  # title, tags, content
  
  shipsXMLRoot=ET.parse("eve-ships2.xml").getroot()
  
  for ship in shipsXMLRoot.findall('Ship'):
    ship.properties={}
    for prop_set in ship.findall("Properties")[0].findall("prop"):
      ship.properties[prop_set.get("Name")]=prop_set.get("Value")
    ships.append(ship)
  
  ShipCatalogue={}

  for ship in ships:
    wikiwords.append(ship.get("Name").lower())
    wikiwords.append(ship.get("Race").lower())
    try:
      ShipCatalogue[ship.get("Type")][ship.get("Race")].append(ship.get("Name"))
    except KeyError:
      try:
        ShipCatalogue[ship.get("Type")][ship.get("Race")]=[ship.get("Name")]
      except KeyError:
        ShipCatalogue[ship.get("Type")]={ship.get("Race"): [ship.get("Name")]}

    wikiwords.append(ship.get("Type").lower())
  
    ship.tags=" ".join(["[[%s]]" % i for i in [ ship.get("Race"), ship.get("Type")]])
    
    ship.details_table="\n".join(["|%s|%s|" % (m, ship.properties[m]) for m in ship.properties.keys()])

    ship.resist_table="\n|Type|EM|Thermal|Kinetic|Explosive|\n|Shield|%s|%s|%s|%s|\n|Armor|%s|%s|%s|%s|\n" %( 
        ship.properties['Shield Em Damage Resistance'] ,
        ship.properties['Shield Thermal Damage Resistance'] ,
        ship.properties['Shield Kinetic Damage Resistance'] ,
        ship.properties['Shield Explosive Damage Resistance'] ,
        ship.properties['Armor Em Damage Resistance'],
        ship.properties['Armor Thermal Damage Resistance'],
        ship.properties['Armor Kinetic Damage Resistance'],
        ship.properties['Armor Explosive Damage Resistance']
        )
    ship.speed_table="\n|Mass|Inertia|Base Speed|Warp Speed|\n|%s|%s|%s|%s|\n" %(
        ship.properties['Mass'],
        ship.properties['Inertia Modifier'],
        ship.properties['Max Velocity'],
        ship.properties['Ship Warp Speed']
        )
  
      # Also ship.get("Race"), Id Description Type 
  
  # Open the ouput wiki. navigate down to teh storeArea (div, id="storeArea")
  infile=BS(open("EveOfflineWiki.html", "r").read())
  outfile=open("Out.html", "w")
  # Add a child div, title=Ship name modifier=Vittorios created, tags changecount="1"
  storeArea=infile.find("div", {"id": "storeArea"})
  # Add a pre tag with the contents being all useful info
  for ship in ships:
    storeArea.append(tiddlerFormat % ( {
      "shipName" : ship.get("Name"),
      "shipTags" : ship.tags,
      "shipDescription" : ship.get("Description"),
      "shipResistTable" : ship.resist_table,
      "shipDetailsTable" : ship.details_table,
      "shipId" : ship.get("Id"),
      "shipSpeedTable" : ship.speed_table,
      }) # End format subs dict
      ) # End append function
  # make sure to [[]] the names of any other ships if you find them.
  # todo: wikify the race, blasters, autocannons, etc

  # Titans are Awesome!
  #Titan Gallente Erebus
  #Titan Caldari Leviathan
  #Titan Amarr Avatar
  #Titan Minmatar Ragnarok

# Now create the "ships" tiddler
  shipsFullListTable=""
  for shipType in ShipCatalogue.keys():
    shipsFullListTable=shipsFullListTable+"\n![[%s]]" % shipType
    for shipRace in ShipCatalogue[shipType].keys():
      shipsFullListTable=shipsFullListTable+"\n[[%s]]:  " %shipRace
      if ShipCatalogue[shipType][shipRace]:
        for shipName in ShipCatalogue[shipType][shipRace]:
          shipsFullListTable=shipsFullListTable+"[[%s]] "%shipName
      else:
        shipsFullListTable=shipsFullListTable+"None"
    shipsFullListTable=shipsFullListTable+"\n"

  #print shipsFullListTable
  storeArea.append(shipsTiddlerFormat % ("Ships", shipsFullListTable))

# Now create a tiddler for each ship type 
  shipsByType={}
  for ship in ships:
    type=ship.get("Type")
    shipsByType.setdefault(type, [])
    shipsByType[type].append(ship)

  for type in shipsByType.keys():
    shipsByTypeTable=""
    shipsByTypeTable=shipsByTypeTable+"\n!![[%s]]" % type
    for ship in shipsByType[type]:
      shipsByTypeTable=shipsByTypeTable+"\n[[%s]] -  [[%s]]" % (ship.get("Name"), ship.get("Race"))
      storeArea.append(shipsTiddlerFormat % (type, shipsByTypeTable))
  
  outfile.write(open("head.dat.html", "r").read())
  outfile.write(storeArea.prettify())
  outfile.write(open("tail.dat.html", "r").read())
  
  outfile.close()
  return ships


if "__main__" in __name__:
  generate_html()



